package com.ruoyi.es.scheduling;

import com.ruoyi.es.entily.Video;
import com.ruoyi.es.mapper.VideoMapper;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Component
public class EsScheduling {

    //同步
    @Autowired
    VideoMapper videoMapper;

    @Resource
    RestHighLevelClient restHighLevelClient;

    @Scheduled(fixedDelay = 50000)
    public void MysqlToEs() throws IOException {
        //先查询
        List<Video> videoList = videoMapper.selectList();
        for (Video video : videoList) {
            IndexRequest indexRequest = new IndexRequest("esvideo", "esvideo");
            indexRequest.id(video.getVid()+"");
            HashMap<String, Object> map = new HashMap<>();
            map.put("number",video.getVideo_number());
            map.put("content",video.getVideo_content());
            map.put("autor",video.getVideo_autor());
            map.put("video",video.getVideo());
            map.put("ctime",video.getCreate_time());
            map.put("utime",video.getUpdate_time());
            map.put("status",video.getVideo_status());
            map.put("del",video.getDel());
            indexRequest.source(map);
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        }
        System.out.println("定时器增量同步！");
    }

}
